package com.zf.zffencheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zf.zffencheng.dao.CapitalOrderProfitDao;
import com.zf.zffencheng.model.pojo.CapitalOrderProfit;
import com.zf.zffencheng.model.pojo.TAccount;
import com.zf.zffencheng.model.pojo.TAdmin;
import com.zf.zffencheng.model.vo.AccountVo;

import com.zf.zffencheng.model.vo.HomePageVo;

import com.zf.zffencheng.service.AdminService;
import com.zf.zffencheng.service.CapitalOrderProfitService;
import com.zf.zffencheng.util.BeanUtils;
import com.zf.zffencheng.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;

/**
 * ClassName: AccountServiceImpl <br/>
 * Description:  <br/>
 * date: 2021/5/11 17:30<br/>
 *
 * @author william <br/>
 */
@Service
public class CapitalOrderProfitServiceImpl extends ServiceImpl<CapitalOrderProfitDao, CapitalOrderProfit> implements CapitalOrderProfitService {
    @Autowired
    private AdminService adminService;

    private static final String shortDateFormat = "yyyy-MM-dd";
    @Override
    public HomePageVo getHomePageVo(Integer adminId,Integer accountId,Integer adminType) {

        HomePageVo homePageVo =new HomePageVo();
        //今日总收益
        QueryWrapper<CapitalOrderProfit> queryWrapper = new QueryWrapper<CapitalOrderProfit>();
        queryWrapper.select("sum(Fee) as fee ");
        queryWrapper
                .eq("AccountId", accountId)
                .eq("Type", adminType)
                .apply("date_format(CreateTime, '%Y-%m-%d') = DATE_FORMAT( {0},'%Y-%m-%d')", new Date());;

        if (getOne(queryWrapper,false) == null){
            homePageVo.setTodayProfit(BigDecimal.ZERO) ;
        }else{
            homePageVo.setTodayProfit(getOne(queryWrapper,false).getFee());
        }

        //昨日收益
        //昨天时间
        Date yesterdayDate = DateUtil.addDays(new Date() ,-1);
        QueryWrapper<CapitalOrderProfit> queryWrapperYesterday = new QueryWrapper<CapitalOrderProfit>();
        queryWrapperYesterday.select("sum(Fee) as fee ");
        queryWrapperYesterday
                .eq("AccountId", accountId)
                .eq("Type", adminType)
                .apply("date_format(CreateTime, '%Y-%m-%d') = DATE_FORMAT( {0},'%Y-%m-%d')", yesterdayDate);

        if (getOne(queryWrapperYesterday,false) == null){
            homePageVo.setYesterdayProfit(BigDecimal.ZERO) ;
        }else{
            homePageVo.setYesterdayProfit(getOne(queryWrapperYesterday,false).getFee());
        }

        //总收益
        QueryWrapper<CapitalOrderProfit> queryWrapperTotal = new QueryWrapper<CapitalOrderProfit>();
        queryWrapperTotal.select("sum(Fee) as fee ");
        queryWrapperTotal
                .eq("AccountId", accountId)
                .eq("Type", adminType);

        if (getOne(queryWrapperTotal,false) == null){
            homePageVo.setTotalProfit(BigDecimal.ZERO) ;
        }else{
            homePageVo.setTotalProfit(getOne(queryWrapperTotal,false).getFee());
        }

        //今日订单
        QueryWrapper<CapitalOrderProfit> queryWrapperTodayOrder = new QueryWrapper<CapitalOrderProfit>();
        queryWrapperTodayOrder.select("COUNT(id) as orderId ");
        queryWrapperTodayOrder
                .eq("AccountId", accountId)
                .eq("Type", adminType)
                .apply("date_format(CreateTime, '%Y-%m-%d') = DATE_FORMAT( {0},'%Y-%m-%d')", new Date());

        if (getOne(queryWrapperTotal,false) == null){
            homePageVo.setTodayOrder(0) ;//今日订单
        }else{
            homePageVo.setTodayOrder(getOne(queryWrapperTotal,false).getOrderId());
        }
        TAdmin admin = adminService.getById(adminId);
        if(null != admin){
            homePageVo.setName(admin.getName());
        }
        return homePageVo;
    }


}
